/*
 * @Author: DUST
 * @Date: 2023-05-17 17:22:43
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-26 14:48:05
 * @FilePath: \vite-project\src\views\admin\tagsManage\index.ts
 * @Description:
 * undefined
 */

import { Ajax, IButton } from "@/interface";
import { modalType } from "@/interface/enum";
import { Ref, ref } from "vue";
import { tagsLIst, addTagApi, editTagApi, delTagApi } from "@/api";
import { message } from "ant-design-vue";
import { AxiosResponse } from "axios";
import { color16 } from "@/utils";

interface tagsItem {
  name: string;
  color?: string;
  id: number;
  powers: number;
  createTime: Date;
  upTime: Date;
}

interface conformObj {
  name: string;
}

class TagsManage {
  /**
   * @description: 标签列表
   */
  private _tagList = ref<Array<tagsItem>>([]);
  public get tagList(): Ref<Array<tagsItem>> {
    return this._tagList;
  }

  /**
   * @description: 抖动控制器
   */
  private _delVisible = ref<boolean>(false);
  public get delVisible(): Ref<boolean> {
    return this._delVisible;
  }
  /**
   * @description: editData
   */
  private _delData = ref<tagsItem | undefined>();
  public get delData(): Ref<tagsItem | undefined> {
    return this._delData;
  }
  /**
   * @description: 抖动控制器
   */
  private _isShake = ref<boolean>(false);
  public get isShake(): Ref<boolean> {
    return this._isShake;
  }

  /**
   * @description: 抖动控制器
   */
  private _shakeIds = ref<Array<number>>([]);
  public get shakeIds(): Ref<Array<number>> {
    return this._shakeIds;
  }
  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _visible = ref<boolean>(false);
  public get visible(): Ref<boolean> {
    return this._visible;
  }
  /**
   * @description: loading
   */
  private _loading = ref<boolean>(false);
  public get loading(): Ref<boolean> {
    return this._loading;
  }
  /**
   * @description: editData
   */
  private _editData = ref<tagsItem | undefined>();
  public get editData(): Ref<tagsItem | undefined> {
    return this._editData;
  }
  /**
   * @description: 新增、编辑景区弹窗控制器
   */
  private _type = ref<modalType>(modalType.ADD);
  public get type(): Ref<modalType> {
    return this._type;
  }
  /**
   * @description: 显示新增弹窗
   */
  public showAdd = () => {
    this._visible.value = true;
    this._type.value = modalType.ADD;
  };
  /**
   * @description: 显示新增弹窗
   */
  public showEdit = (item: tagsItem) => {
    this._visible.value = true;
    this._editData.value = item;
    this._type.value = modalType.EDIT;
  };
  /**
   * @description: 按钮列表
   */
  private _buttonList = ref<Array<IButton>>([
    {
      name: "新增标签",
      type: "primary",
      func: this.showAdd,
    },
  ]);
  public get buttonList(): Ref<Array<IButton>> {
    return this._buttonList;
  }
  /**
   * @description: 获取tags列表
   */
  public getData = () => {
    tagsLIst().then((res: AxiosResponse<Ajax<Array<tagsItem>>>) => {
      console.log(res);
      res.data.data.filter((val) => {
        val.color = color16();
      });
      this._tagList.value = res.data.data;
      console.log(this._tagList.value)
    });
  };
  constructor() {
    this.getData();
  }
  /**
   * @description: 抖动
   */
  public doShake = (item: tagsItem) => {
    this._shakeIds.value.push(item.id);
    if (!this._isShake.value) {
      this._isShake.value = true;
      const timer = setTimeout(() => {
        clearTimeout(timer);
        this._isShake.value = false;
      }, 820);
    }
    const timer = setTimeout(() => {
      clearTimeout(timer);
      this._shakeIds.value.pop();
    }, 820);
  };
  /**
   * @description: 长按事件
   * @param {tagsItem} item: 选中的节点
   */
  private pressDownTime: number = 0;
  private pressTimer: any = null;
  public pressHold = (item: tagsItem) => {
    this.pressDownTime = Number(new Date());
    console.log(this.pressDownTime);
    this.pressTimer = setInterval(() => {
      if (Date.now() - this.pressDownTime >= 2 * 1000) {
        console.log("长按了2秒", item);
        this._delVisible.value = true;
        this._delData.value = item;
        clearInterval(this.pressTimer);
      }
    }, 200);
  };
  public cancelPop = () => {
    this._delVisible.value = false;
    this._delData.value = undefined;
  }
  public confirmDel = () => {
    this._delVisible.value = false;
    delTagApi({ id: this._delData.value?.id }).then(() => {
      message.success("删除成功");
      this.getData();
    });
  };
  public pressUp = () => {
    // const upTime = Date.now();c
    clearInterval(this.pressTimer);
    this.pressTimer = null;
    // if(upTime - pressDownTime)
  };
  /**
   * @description: 新增标签
   * @param {conformObj} params：新增参数
   */
  public addFunc(params: conformObj) {
    addTagApi(params)
      .then(() => {
        message.success("新增成功");
        this._visible.value = false;
        this.getData();
      })
      .finally(() => {
        this._loading.value = false;
      });
  }
  /**
   * @description: 编辑标签
   * @param {conformObj} params：编辑参数
   */
  public editFunc(params: conformObj) {
    editTagApi(params)
      .then(() => {
        message.success("编辑成功");
        this._visible.value = false;
        this.getData();
      })
      .finally(() => {
        this._loading.value = false;
      });
  }
  /**
   * @description: 确认提交
   * @param {object} param1
   */
  public doOk = ({ type, obj }: { type: modalType; obj: conformObj }) => {
    this._loading.value = true;
    if (type == modalType.ADD) this.addFunc(obj);
    else if (type == modalType.EDIT) this.editFunc(obj);
  };
}

export default TagsManage;
